/**
 * 
 */
package org.grill.clovercash;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.grill.clovercash.accounts.Account;

final class EditAccountAction extends Action {
	private AccountListView view;

	EditAccountAction(AccountListView view) {
		super("Edit...");
		this.view = view;
	}

	@Override
	public void run() {
		Account receiver;
		IStructuredSelection selection = (IStructuredSelection) view.getViewer().getSelection();
		if (selection.isEmpty())
			return;
		receiver = (Account) selection.getFirstElement();
		
		AccountEditDialog dlg = new AccountEditDialog(view.getSite().getShell());
		dlg.setName(receiver.getName());
		dlg.setDescription(receiver.getDescription());
		dlg.setType(receiver.getType());
		
		if (dlg.open() == IDialogConstants.OK_ID) {
			receiver.setName(dlg.getName());
			receiver.setDescription(dlg.getDescription());
			receiver.setType(dlg.getType());
			view.getViewer().refresh(CloverCashUIPlugin.getDefault().getRootAccount());
		}
	}
}